﻿using FarPoint.Web.Spread;
using FarPoint.Web.Spread.Model;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.OrderEntry
{
  public partial class Checkout : System.Web.UI.Page
  {
    StyleInfo style = new StyleInfo();
    protected System.Web.UI.WebControls.HyperLink HyperLink1;
    // FpBorder()
    Border border = new Border();


    private void Page_Load(System.Object sender, System.EventArgs e)
    {
      //Put user code to initialize the page here

      if (!Page.IsPostBack)
      {
          this.FpSpread1.UseClipboard = false;
          PopulateSpread();
      }

      try
      {
        if (ckShowSpread.Checked == true)
        {
          //Show Spread
          //FpSpread1.Sheets[0].ColumnHeaderVisible = True
          FpSpread1.Sheets[0].ColumnHeader.Visible = true;
          FpSpread1.Sheets[0].RowHeader.Visible = true;
          //FpSpread1.Sheets[0].RowHeaderVisible = True
          FpSpread1.Sheets[0].GridLines = GridLines.Both;
        }
        else
        {
          //Hide Spread
          FpSpread1.Sheets[0].ColumnHeader.Visible = false;
          FpSpread1.Sheets[0].RowHeader.Visible = false;
          FpSpread1.Sheets[0].GridLines = GridLines.None;
        }

      }
      catch
      {
      }

      if (this.InIE9PlusCompatView())
      {
        foreach (FarPoint.Web.Spread.FpSpread spread in this.GetSpreads(this))
        {
          FarPoint.Web.Spread.SheetView activeSheet = spread.ActiveSheetView;
          FarPoint.Web.Spread.Border border = activeSheet.DefaultStyle.Border;
          if (border == null || border.IsEmpty)
          {
            activeSheet.DefaultStyle.Border = new FarPoint.Web.Spread.Border(BorderStyle.Solid, activeSheet.GridLineColor, 1);
          }
        }
      }

    }

    private bool InIE9PlusCompatView()
    {
      if (this.Page.Request.Browser.MajorVersion == 7)
      {
        string userAgent = this.Page.Request.UserAgent;
        int index = userAgent.IndexOf("trident/", StringComparison.OrdinalIgnoreCase);
        int index2 = 0;
        if (index >= 0)
        {
          index2 = userAgent.IndexOf(";", index);
        }
        else
        {
          index2 = -1;
        }
        if (index2 > index && index >= 0)
        {
          double tridentVersion = 0;
          if (double.TryParse(userAgent.Substring(index + 8, index2 - (index + 8)), out tridentVersion))
          {
            return tridentVersion >= 5;
          }
        }
      }
      return false;
    }

    private System.Collections.IEnumerable GetSpreads(Control ctrl)
    {
      System.Collections.Generic.List<FarPoint.Web.Spread.FpSpread> list = new System.Collections.Generic.List<FarPoint.Web.Spread.FpSpread>();
      foreach (Control c in ctrl.Controls)
      {
        foreach (FarPoint.Web.Spread.FpSpread spread in GetSpreads(c))
        {
          list.Add(spread);
        }

        if (c is FarPoint.Web.Spread.FpSpread)
        {
          list.Add((FarPoint.Web.Spread.FpSpread)c);
        }
      }

      return list;
    }

    public void PopulateSpread()
    {
      DefaultSheetDataModel model = new DefaultSheetDataModel(16, 4);
      //rows, columns

      FpSpread1.Sheets[0].DataModel = model;
      FpSpread1.Sheets[0].PageSize = 25;
      //Set Column Widths
      FpSpread1.Sheets[0].SetColumnWidth(0, 155);
      FpSpread1.Sheets[0].SetColumnWidth(1, 100);
      FpSpread1.Sheets[0].SetColumnWidth(2, 100);
      FpSpread1.Sheets[0].SetColumnWidth(3, 100);
      //Set Row Height
      FpSpread1.Sheets[0].SetRowHeight(15, 70);

      FpSpread1.Sheets[0].ColumnHeader.Visible = false;
      FpSpread1.Sheets[0].RowHeader.Visible = false;
      FpSpread1.Sheets[0].GridLines = GridLines.None;
      FpSpread1.HorizontalScrollBarPolicy = ScrollBarPolicy.AsNeeded;
      FpSpread1.VerticalScrollBarPolicy = ScrollBarPolicy.AsNeeded;

      style = new StyleInfo();
      style.Reset();
      style.Font.Size = FontUnit.Point(10);
      style.Font.Name = "Verdana";
      //style.Locked = True
      FpSpread1.Sheets[0].SetStyleInfo(-1, -1, style);

      model.SetValue(0, 1, "联系信息");
      FpSpread1.Sheets[0].SpanModel.Add(0, 1, 1, 3);
      SetHeaderStyle(0, -1);

      model.SetValue(1, 0, "姓名:");
      FpSpread1.Sheets[0].SpanModel.Add(1, 2, 1, 2);
      SetHeaderStyle(1, 0);

      model.SetValue(2, 0, "公司:");
      FpSpread1.Sheets[0].SpanModel.Add(2, 1, 1, 3);
      SetHeaderStyle(2, 0);

      model.SetValue(3, 0, "地址:");
      FpSpread1.Sheets[0].SpanModel.Add(3, 1, 1, 3);
      SetHeaderStyle(3, 0);

      model.SetValue(4, 0, "家庭住址:");
      SetHeaderStyle(4, 0);

      model.SetValue(5, 0, "电话:");
      FpSpread1.Sheets[0].SpanModel.Add(5, 1, 1, 3);
      SetHeaderStyle(5, 0);

      //Lock Cells
      LockRow(6);

      model.SetValue(7, 1, "礼品");
      FpSpread1.Sheets[0].SpanModel.Add(7, 1, 1, 3);
      SetHeaderStyle(7, -1);

      model.SetValue(8, 0, "礼品");
      FpSpread1.Sheets[0].SpanModel.Add(8, 1, 1, 3);
      SetHeaderStyle(8, 0);

      //Lock Cells
      LockRow(9);

      model.SetValue(10, 1, "海运");
      SetHeaderStyle(10, -1);

      model.SetValue(11, 0, "运输经由");
      FpSpread1.Sheets[0].SpanModel.Add(11, 1, 1, 3);
      SetHeaderStyle(11, 0);

      model.SetValue(12, 0, "服务");
      FpSpread1.Sheets[0].SpanModel.Add(12, 1, 1, 3);
      SetHeaderStyle(12, 0);

      //Lock Cells
      LockRow(13);

      model.SetValue(14, 1, "付款");
      SetHeaderStyle(14, -1);

      model.SetValue(15, 0, "付款方式");
      FpSpread1.Sheets[0].SpanModel.Add(15, 1, 1, 3);
      SetHeaderStyle(15, 0);

      //Styles
      style = new StyleInfo();
      style.Reset();
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(1, 1, style);
      // First Name
      FpSpread1.ActiveSheetView.SetStyleInfo(1, 2, style);
      //Last Name
      FpSpread1.ActiveSheetView.SetStyleInfo(2, 1, style);
      //Company
      FpSpread1.ActiveSheetView.SetStyleInfo(3, 1, style);
      //Address
      FpSpread1.ActiveSheetView.SetStyleInfo(4, 1, style);
      //City
      FpSpread1.ActiveSheetView.SetStyleInfo(4, 2, style);
      //State
      FpSpread1.ActiveSheetView.SetStyleInfo(4, 3, style);
      //Zip
      FpSpread1.ActiveSheetView.SetStyleInfo(5, 1, style);
      //Phone

      // Image cell        
      //Contact
      style = new StyleInfo();
      style.Reset();
      style.CellType = new ImageCellType("images/contactinfo.gif");
      style.HorizontalAlign = HorizontalAlign.Right;
      FpSpread1.ActiveSheetView.SetStyleInfo(0, 0, style);

      //Gift
      style = new StyleInfo();
      style.Reset();
      style.CellType = new ImageCellType("images/giftsm.gif");
      style.HorizontalAlign = HorizontalAlign.Right;
      FpSpread1.ActiveSheetView.SetStyleInfo(7, 0, style);

      //Shipping
      style = new StyleInfo();
      style.Reset();
      style.CellType = new ImageCellType("images/globe.gif");
      style.HorizontalAlign = HorizontalAlign.Right;
      FpSpread1.ActiveSheetView.SetStyleInfo(10, 0, style);

      //Payment
      style = new StyleInfo();
      style.Reset();
      style.CellType = new ImageCellType("images/creditcard.jpg");
      style.HorizontalAlign = HorizontalAlign.Right;
      FpSpread1.ActiveSheetView.SetStyleInfo(14, 0, style);

      //Gift
      //Checkbox
      style = new StyleInfo();
      style.Reset();
      style.CellType = new CheckBoxCellType("images/checked.gif", "images/unchecked.gif");
      //style.CellType = New FpCheckBoxType()
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(8, 1, style);

      //Ship via
      // ComboBox
      style = new StyleInfo();
      style.Reset();
      style.CellType = new ComboBoxCellType(new string[] {
				"FedEx",
				"UPS",
				"Other"
			});
      style.Font.Size = FontUnit.Point(10);
      style.Font.Name = "Verdana";
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(11, 1, style);

      //Service
      //Radio
      style = new StyleInfo();
      style.Reset();
      style.CellType = new RadioButtonListCellType(new string[] {
				"Ground",
				"2-Day",
				"Overnight"
			});
      style.Font.Size = FontUnit.XSmall;
      //FontUnit.Parse(10)
      style.Font.Name = "Verdana";
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(12, 1, style);
      //FpSpread1.ActiveSheetView.SetColumnWidth(1, 75) 'Book Image
      FpSpread1.ActiveSheetView.SetRowHeight(12, 35);

      //Payment
      // ListBox cell         
      style = new StyleInfo();
      style.Reset();
      style.CellType = new ListBoxCellType(new string[] {
				"Visa",
				"MC",
				"AMEX"
			});
      style.Font.Size = FontUnit.Point(10);
      style.Font.Name = "Verdana";
      //Add border style
      AddBorder();
      FpSpread1.ActiveSheetView.SetStyleInfo(15, 1, style);

    }
    public void SetHeaderStyle(int row, int col)
    {
      //Header Column
      style = new StyleInfo();
      style.Reset();
      style.CellType = new LabelCellType();

      style.BackColor = Color.LightSlateGray;
      style.ForeColor = Color.White;

      if (col == 0)
      {
        style.HorizontalAlign = HorizontalAlign.Right;
      }
      else
      {
        style.Font.Size = FontUnit.Point(10);
        style.Font.Bold = true;
        style.ForeColor = Color.Wheat;
      }

      FpSpread1.Sheets[0].SetStyleInfo(row, col, style);

    }

    public void LockRow(int row)
    {
      //Span Cells
      FpSpread1.Sheets[0].SpanModel.Add(row, 0, 1, 4);

      style = new StyleInfo();
      style.Locked = true;
      FpSpread1.ActiveSheetView.SetStyleInfo(row, -1, style);
    }

    public void AddBorder()
    {
      //Add border around the cell
      border = border.Clone() as Border;
      border.BorderStyle = BorderStyle.Solid;
      border.BorderColor = Color.Silver;
      border.BorderSize = 1;
      style.Border = border;
    }
  }
}